package com.desksoft.ecommerce.handler.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 
 * 创建作者：luoy
 * 文件名称：spring代理的Servlet
 * 版本：
 * 功能：
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public class ServletToBeanProxy extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -970905828783620036L;

	protected final Log log = LogFactory.getLog(getClass());
	
	private String targetBean;
	private Servlet proxy;

	public void init() throws ServletException {
		log.debug("proxy init");
		this.targetBean = getInitParameter("targetBean");
		getServletBean();
		proxy.init(getServletConfig());
	}

	public void service(ServletRequest req, ServletResponse res)
			throws ServletException, IOException {
		proxy.service(req, res);
	}

	private void getServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		this.proxy = (Servlet) wac.getBean(targetBean);
	}

}
